package sanrenxing.kechenglx.servlet;

import com.google.gson.Gson;
import sanrenxing.kechenglx.dao.CourseSubjectDao;
import sanrenxing.kechenglx.dao.Impl.CourseSubjectDaoImpl;
import sanrenxing.kechenglx.domian.CourseSubject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * 功能：
 * 编写者：蒋玲苹
 * 日期：20201014
 * 版本：1.0
 */
@WebServlet("/CourseSubjectServlet")
public class CourseSubjectServlet extends HttpServlet {
    //创建业务层操作对象
    CourseSubjectDao fpd=new CourseSubjectDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置请求字符集
        req.setCharacterEncoding("utf-8");
        //设置响应字符集和内容类型
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //获取请求路径中的操作方式
        String method=req.getParameter("method");

        if (method.equals("findSubjects")){
            //查询科目对应题目
            this.findSubjcts(req,resp);
        }else if (method.equals("saveCourseMsg")){
            //保存课程练习的信息
            this.saveCourseMsg(req,resp);
        }

    }

    /**
     * 保存课程练习的信息
     * @param req
     * @param resp
     */
    private void saveCourseMsg(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter out=resp.getWriter();
        //接收来自客户端的课程编号
        String courseId=req.getParameter("courseId");
        //接收来自客户端的顺序练习或者随机练习
        String practiceType=req.getParameter("practiceType");
        if (courseId!=null && practiceType!=null){
            //创建session,将其保存到session当中
            HttpSession session=req.getSession();
            //把课程编号保存到session中
            session.setAttribute("courseId",courseId);
            //把练习模式保存到session中
            session.setAttribute("practiceType",practiceType);
            out.print("ok");
        }else{
            out.print("no");
        }

    }

    /**
     * 查询科目对应题目
     * @param req
     * @param resp
     */
    private void findSubjcts(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        //创建输出流对象
        PrintWriter out=resp.getWriter();
        //创建session对象
        HttpSession session=req.getSession();
        //创建业务层对象
        CourseSubjectDao cpd=new CourseSubjectDaoImpl();
        //通过session得到课程id
        String sub=(String) session.getAttribute("courseId");
        //通过session得到practiceType是顺序还是随机
        String practiceType=(String) session.getAttribute("practiceType");
        //用题目编号接收转换为int类型的sub
        int subjectId=Integer.parseInt(sub);
        //获取题目编号
        List<Integer> map=cpd.subjectNum(subjectId);
        // 创建一个保存题目的数组
        List<CourseSubject> courseSubjectList=new ArrayList<>();
        for(int arr:map){
            // 调用数据层找题目的方法并用cpd把题目添加到集合中
            courseSubjectList.add(cpd.getSubject(arr));
        }

        //判断选择的是否为B随机练习，如果是随机练习则打乱题目顺序
        if (practiceType.equals("B")){
            //通过Collections.shuffle打乱题目顺序
            Collections.shuffle(courseSubjectList);
        }
        // 创建map集合
        Map<String,Object> maps=new HashMap<>();
        //将数据保存到集合中
        maps.put("data",courseSubjectList);
        for (CourseSubject keys:courseSubjectList){
            System.out.print("题号："+keys.getCourseId());
            System.out.print("题目："+keys.getStem());
            System.out.print("A："+keys.getOptionA());
            System.out.print("B："+keys.getOptionB());
            System.out.print("C："+keys.getOptionC());
            System.out.print("D："+keys.getOptionD());
            System.out.print("E："+keys.getOptionE());
            System.out.print("F："+keys.getOptionF());
            System.out.print("G："+keys.getOptionG());
            System.out.print("H："+keys.getOptionH());
            System.out.print("答案："+keys.getCorrect());
        }
        //新建Gson对象
        Gson gson=new Gson();
        // 将数据装换成json数据格式
        String json=gson.toJson(maps);
        // 发送数据
        out.print(json);
        System.out.println(json);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doGet(req,resp);
    }
}
